/**
 * @FileName: cookies
 * @Author: mazaiting
 * @Date: 2018/8/12
 * @Description:
 */
export default class Cookies {
  /**
   * 获取Cookie
   * @param name 名称
   */
  static getCookie (name) {
    let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    let arr = document.cookie.match(reg);
    if (arr) {
      return arr[2];
    } else {
      return null;
    }
  }

  /**
   * 设置Cookie
   * @param name 名称
   * @param value 值
   * @param expireTimes 过期时间, 秒数
   */
  static setCookie (name, value, expireTimes) {
    let date = new Date();
    date.setTime(date.getTime() + expireTimes * 1000);
    document.cookie = name + "=" + encodeURIComponent(value) + ((expireTimes == null) ? "" : ";expires=" + date.toUTCString());
  }

  /**
   * 删除Cookie
   * @param name Cookie名字
   */
  static deleteCookie (name) {
    let date = new Date();
    date.setTime(date.getTime() - 1);
    let oldCookie = this.getCookie(name);
    if (null != oldCookie) document.cookie = name + "=" + oldCookie + ";expires=" + date.toUTCString();
  }
}
